package com.xujie.sportsmeeting.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xujie.sportsmeeting.common.result.Result;
import com.xujie.sportsmeeting.dto.JudgeDTO;
import com.xujie.sportsmeeting.dto.JudgeQueryDTO;
import com.xujie.sportsmeeting.entity.Judge;
import com.xujie.sportsmeeting.service.JudgeService;
import com.xujie.sportsmeeting.vo.JudgeVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 裁判员管理控制器
 */
@Tag(name = "裁判员管理", description = "裁判员管理相关接口")
@RestController
@RequestMapping("/judge")
@RequiredArgsConstructor
public class JudgeController {

    private final JudgeService judgeService;

    @Operation(summary = "获取裁判员列表")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('judge') or authentication.name == 'admin'")
    public Result<IPage<JudgeVO>> getJudgeList(JudgeQueryDTO queryDTO) {
        IPage<Judge> page = new Page<>(queryDTO.getCurrent(), queryDTO.getSize());
        IPage<Judge> judgePage = judgeService.getJudgePage(page, queryDTO);
        
        // 转换为VO
        IPage<JudgeVO> voPage = judgePage.convert(judge -> {
            JudgeVO vo = new JudgeVO();
            BeanUtils.copyProperties(judge, vo);
            return vo;
        });
        
        return Result.success(voPage);
    }

    @Operation(summary = "获取裁判员详情")
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('judge') or authentication.name == 'admin'")
    public Result<JudgeVO> getJudgeDetail(@PathVariable Long id) {
        Judge judge = judgeService.getById(id);
        if (judge == null) {
            return Result.error("裁判员不存在");
        }
        
        JudgeVO vo = new JudgeVO();
        BeanUtils.copyProperties(judge, vo);
        
        return Result.success(vo);
    }

    @Operation(summary = "创建裁判员")
    @PostMapping
    @PreAuthorize("hasAuthority('judge:add') or authentication.name == 'admin'")
    public Result<Void> createJudge(@Valid @RequestBody JudgeDTO judgeDTO) {
        Judge judge = new Judge();
        BeanUtils.copyProperties(judgeDTO, judge);
        
        boolean success = judgeService.createJudge(judge);
        if (success) {
            return Result.success("裁判员创建成功");
        } else {
            return Result.error("裁判员创建失败");
        }
    }

    @Operation(summary = "更新裁判员信息")
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('judge:edit') or authentication.name == 'admin'")
    public Result<Void> updateJudge(@PathVariable Long id, @Valid @RequestBody JudgeDTO judgeDTO) {
        Judge judge = new Judge();
        BeanUtils.copyProperties(judgeDTO, judge);
        judge.setId(id);
        
        boolean success = judgeService.updateJudge(judge);
        if (success) {
            return Result.success("裁判员更新成功");
        } else {
            return Result.error("裁判员更新失败");
        }
    }

    @Operation(summary = "删除裁判员")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('judge:delete') or authentication.name == 'admin'")
    public Result<Void> deleteJudge(@PathVariable Long id) {
        boolean success = judgeService.removeById(id);
        if (success) {
            return Result.success("裁判员删除成功");
        } else {
            return Result.error("裁判员删除失败");
        }
    }

    @Operation(summary = "批量删除裁判员")
    @DeleteMapping("/batch")
    @PreAuthorize("hasAuthority('judge:delete') or authentication.name == 'admin'")
    public Result<Void> batchDeleteJudges(@RequestBody List<Long> ids) {
        boolean success = judgeService.removeByIds(ids);
        if (success) {
            return Result.success("批量删除成功");
        } else {
            return Result.error("批量删除失败");
        }
    }

    @Operation(summary = "启用/禁用裁判员")
    @PutMapping("/{id}/status")
    @PreAuthorize("hasAuthority('judge:edit') or authentication.name == 'admin'")
    public Result<Void> toggleJudgeStatus(@PathVariable Long id, @RequestBody StatusRequest request) {
        boolean success = judgeService.updateJudgeStatus(id, request.getStatus());
        if (success) {
            return Result.success("裁判员状态更新成功");
        } else {
            return Result.error("裁判员状态更新失败");
        }
    }

    // 内部请求类
    public static class StatusRequest {
        private Integer status;
        
        public Integer getStatus() {
            return status;
        }
        
        public void setStatus(Integer status) {
            this.status = status;
        }
    }
}
